//
//  RecommenDetailViewController.m
//  礼物
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "RecommenDetailViewController.h"
#import "RecommendOfCommentTableViewCell.h"
#import "RecommendOfIntroduceTableViewCell.h"
#import "RecommenDetailModel.h"
@interface RecommenDetailViewController ()

@end

@implementation RecommenDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"商品详情";
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    //默认为图文介绍
    self.isSelected = YES;

    self.myTableView=[[UITableView alloc]initWithFrame:MAINSCREEN];
    self.myTableView.dataSource=self;
    self.myTableView.delegate=self;
    self.myTableView.sectionHeaderHeight=30*HEIGHT/667;
    [self.view addSubview:self.myTableView];
    UIView *bView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 500*HEIGHT/667)];
    self.myTableView.tableHeaderView=bView;
    self.lunboScrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 300*HEIGHT/667)];
    self.lunboScrollview.contentSize=CGSizeMake(WIDTH * self.model.image_urls.count, 0);
    self.lunboScrollview.pagingEnabled = YES;
    self.lunboScrollview.contentOffset=CGPointMake(WIDTH, 0);
    self.lunboScrollview.showsHorizontalScrollIndicator=NO;
    self.lunboScrollview.userInteractionEnabled=YES;
    self.lunboScrollview.delegate=self;
    [bView addSubview:self.lunboScrollview];
    [self  setLunboPicture1];
    UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(10*WIDTH/375, self.lunboScrollview.frame.origin.y+self.lunboScrollview.frame.size.height+8*HEIGHT/667, WIDTH, 30*HEIGHT/667)];
    nameLabel.text=self.model.name;
    [bView addSubview:nameLabel];
    UILabel *priceLabel=[[UILabel alloc]initWithFrame:CGRectMake(10*WIDTH/375, nameLabel.frame.origin.y+nameLabel.frame.size.height+2*HEIGHT/667, WIDTH, 20*HEIGHT/667)];
    priceLabel.textColor=[UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:0.840];
    priceLabel.text=[NSString stringWithFormat:@"￥%@",self.model.price];
    [bView addSubview:priceLabel];
    
    UILabel *description1Label=[[UILabel alloc]init];
    description1Label.text=self.model.description1;
    description1Label.numberOfLines=0;
    description1Label.font=[UIFont systemFontOfSize:14];
    CGSize constraint=CGSizeMake(300*WIDTH/375, 20000.0f);
    CGSize size=[self.model.description1 sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    [description1Label setFrame:CGRectMake(10*WIDTH/375, priceLabel.frame.origin.y+priceLabel.frame.size.height+5*HEIGHT/667, (375-10)*WIDTH/375, size.height)];
    [bView addSubview:description1Label];
    self.recommenDetailOneCellArray=[NSMutableArray array];
    [AppToolss getAFNetWorkingData:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items/%ld", self.model.id] andBlock:^(id data) {
        
        NSMutableDictionary *dictionary = data;
        
        NSMutableDictionary *dic = [dictionary objectForKey:@"data"];
        
        RecommenDetailOneModel *recommendDetailModel = [[RecommenDetailOneModel alloc] init];
        [recommendDetailModel setValuesForKeysWithDictionary:dic];
        recommendDetailModel.descriptions = [dic objectForKey:@"description"];
        
        [self.recommenDetailOneCellArray addObject:recommendDetailModel];
        [self.myTableView reloadData];

    } andError:^(id failure) {}];
    
    self.pingLunArray=[NSMutableArray array];
    [AppToolss getAFNetWorkingData:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items/%ld/comments?offset=0&limit=20",self.model.id] andBlock:^(id data) {
        NSMutableDictionary *Dic=data;
        NSMutableDictionary *Dic1=[Dic objectForKey:@"data"];
        NSMutableArray *array=[Dic1 objectForKey:@"comments"];
        for (NSMutableDictionary *dic in array) {
            RecommenDetailModel *model=[[RecommenDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            NSMutableDictionary *dic1=[dic objectForKey:@"user"];
            model.nickname=[dic1 objectForKey:@"nickname"];
            model.avatar_url=[dic1 objectForKey:@"avatar_url"];
            [self.pingLunArray addObject:model];
        }
        [self.myTableView reloadData];
    } andError:^(id failure) {}];
    
    UIView *aView=[[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-49, WIDTH, 49)];
    aView.backgroundColor=[UIColor whiteColor];
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button3.frame = CGRectMake(WIDTH / 5, 8, WIDTH*3 / 5, 33);
    self.button3.backgroundColor = [UIColor colorWithRed:38.0/255.0 green:215.0/255.0 blue:164.0/255.0 alpha:0.680];
    [self.button3 setTitle:@"前往购买" forState:UIControlStateNormal];
//    self.button3.tintColor=[UIColor whiteColor];
    [self.button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [aView addSubview:self.button3];
    self.button3.layer.masksToBounds=YES;
    self.button3.layer.cornerRadius=self.button3.frame.size.width/15;
    [self.view addSubview:aView];
    
    
}
-(void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30*HEIGHT/667)];
    
    //图文介绍
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button1.frame = CGRectMake(0, 0, WIDTH / 2, myView.frame.size.height);
    self.button1.backgroundColor = RGBA(247, 247, 247, 1);
    [self.button1 setTitle:@"图文介绍" forState:UIControlStateNormal];
    [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button1 addTarget:self action:@selector(introduceButton:) forControlEvents:UIControlEventTouchUpInside];
    [myView addSubview:self.button1];
    
    
    //灰线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 2, 0, 1, myView.frame.size.height)];
    lineView.backgroundColor = [UIColor colorWithRed:38.0/255.0 green:215.0/255.0 blue:164.0/255.0 alpha:0.680];
    [myView addSubview:lineView];
    
    //攻略
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button2.frame = CGRectMake(WIDTH / 2 + 1, 0, WIDTH / 2 - 1, myView.frame.size.height);
    self.button2.backgroundColor = RGBA(247, 247, 247, 1);
    [self.button2 setTitle:@"评论" forState:UIControlStateNormal];
    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button2 addTarget:self action:@selector(CommentButton:) forControlEvents:UIControlEventTouchUpInside];
    [myView addSubview:self.button2];
    
    
    self.button1.layer.masksToBounds=YES;
    self.button1.layer.borderColor=[UIColor colorWithRed:38.0/255.0 green:215.0/255.0 blue:164.0/255.0 alpha:0.680].CGColor;
    self.button1.layer.borderWidth=0.3;
    self.button2.layer.masksToBounds=YES;
    self.button2.layer.borderColor=[UIColor colorWithRed:38.0/255.0 green:215.0/255.0 blue:164.0/255.0 alpha:0.680].CGColor;
    self.button2.layer.borderWidth=0.3;
    return myView;

    
}

-(void)buttonAction:(UIButton *)button
{
    self.myWebView=[[UIWebView alloc]initWithFrame:MAINSCREEN];
    self.myWebView.delegate=self;
    NSURL *url=[NSURL URLWithString:self.model.purchase_url];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
   
    [self.myWebView loadRequest:request];
    [self.view addSubview:self.myWebView];

    
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    self.hub=[[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.hub];
    self.hub.mode=MBProgressHUDModeIndeterminate;
    self.hub.delegate=self;
    self.hub.labelText=@"正在加载";
    self.hub=[MBProgressHUD showHUDAddedTo:self.view animated:YES];

}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
        self.hub.hidden=YES;
    
}

-(void)introduceButton:(UIButton *)button1
{
    self.isSelected = YES;
    [self.myTableView reloadData];
    
}
-(void)CommentButton:(UIButton *)button2
{
    self.isSelected = NO;
    [self.myTableView reloadData];
    
}

//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isSelected == YES)
    {
        return HEIGHT;
    }
    else
    {
        return 100*HEIGHT/667;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isSelected == YES)
    {
        return self.recommenDetailOneCellArray.count;
    }
    else
    {
        return self.pingLunArray.count;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.isSelected == YES)
    {
        //创建自定义cell
        static NSString *cellIndentifier = @"cellone";
        RecommendOfIntroduceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil)
        {
            cell = [[RecommendOfIntroduceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;//设置cell点击效果 (没有置灰效果)
        RecommenDetailOneModel *model = [self.recommenDetailOneCellArray objectAtIndex:indexPath.row];
        cell.model = model;
        
        return cell;
    }
    else
    {
        //创建自定义cell
        static NSString *cellIndentifier = @"celltwo";
        RecommendOfCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if (cell == nil)
        {
            cell = [[RecommendOfCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        }
        
        //恢复cell线
        self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        //设置cell点击效果 (没有置灰效果)
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        RecommenDetailModel *model = [self.pingLunArray objectAtIndex:indexPath.row];
        cell.model = model;
        return cell;
    }
}


-(void)setLunboPicture1
{
    self.array=[NSMutableArray arrayWithArray:self.model.image_urls];
    id imageFirst1 = [self.array firstObject];
    id imageLast1 = [self.array lastObject];
    [self.array insertObject:imageLast1 atIndex:0];
    [self.array addObject:imageFirst1];
    
    for (int index = 0; index < self.array.count; index++)
    {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.lunboScrollview.frame.origin.x + self.lunboScrollview.frame.size.width * index, 0, self.lunboScrollview.frame.size.width, self.lunboScrollview.frame.size.height)];
        NSString *url=[self.array objectAtIndex:index];
        [imageView sd_setImageWithURL:[NSURL URLWithString:url]];
        imageView.userInteractionEnabled = YES;
        [self.lunboScrollview addSubview:imageView];
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.lunboScrollview.contentSize = CGSizeMake(WIDTH * self.array.count, 0);
    
    if (self.lunboScrollview==scrollView)
    {
        
        if (self.lunboScrollview.contentOffset.x >= self.lunboScrollview.frame.size.width * (self.array.count - 1))
        {
            //            NSLog(@"最后一页");//循环滚动
            [self.lunboScrollview setContentOffset:CGPointMake(self.lunboScrollview.frame.size.width, 0) animated:NO];
        }
        if (self.lunboScrollview.contentOffset.x <= 0)
        {
            [self.lunboScrollview setContentOffset:CGPointMake(self.lunboScrollview.frame.size.width * (self.array.count - 2), 0) animated:NO];
        }
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
